package cn.tedu.ivos.vehicle.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQueryDTO;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveOrUpdateParamDTO;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Stack;

@Slf4j
@RestController
@RequestMapping(value = "/v1/vehicle")
public class VehicleController {

    @Autowired
    VehicleService vehicleService;

    @GetMapping("/selectVehicles")
    public JsonResult selectVehicle(VehicleQueryDTO vehicleQuery){
        log.debug("VehicleController-查询车辆:VehicleQueryDTO={}",vehicleQuery);
        List<VehicleVO> list = vehicleService.selectVehicle(vehicleQuery);
        log.debug("VehicleController-查询车辆:list={}",list);
        return JsonResult.ok(list);
    }

    @GetMapping("/select")
    public JsonResult selectVehicles(VehicleQueryDTO vehicleQuery){
        log.debug("VehicleController-查询车辆:VehicleQueryDTO={}",vehicleQuery);
        List<VehicleVO> list = vehicleService.selectVehicle(vehicleQuery);
        log.debug("VehicleController-查询车辆:list={}",list);
        return JsonResult.ok(list);
    }

    /** "新增车辆信息" 的控制层方法: saveVehicle()*/
    //注意：前端el-form表单传过来的时间格式是string类型，
    //所以需要在VehicleSaveOrUpdateParamDTO中用@DateTimeFormat格式化注解将时间转化为Date类型
    //才能封装到VehicleSaveOrUpdateParamDTO对象中(否则会抛出TypeMisMatchException，报400异常)
    @PostMapping("/saveOrUpdateVehicle")
    public JsonResult saveOrUpdateVehicle(VehicleSaveOrUpdateParamDTO vehicleSaveParam){
        log.debug("VehicleController-保存车辆信息：VehicleSaveParamDTO={}",vehicleSaveParam);
        String msg=vehicleService.saveOrUpdateVehicle(vehicleSaveParam);
        return JsonResult.ok(StatusCode.SUCCESS.getCode(),msg);
    }

    @PostMapping("/save")//上上一个方法一模一样
    public JsonResult saveOrUpdateVehicles(VehicleSaveOrUpdateParamDTO vehicleSaveParam){
        log.debug("VehicleController-保存车辆信息：VehicleSaveParamDTO={}",vehicleSaveParam);
        String msg=vehicleService.saveOrUpdateVehicle(vehicleSaveParam);
        return JsonResult.ok(StatusCode.SUCCESS.getCode(),msg);
    }


    @GetMapping("/deleteVehicle/{vehicleId}")
    public JsonResult deleteVehicle(@PathVariable("vehicleId") Long vehicleId){
        log.info("VehicleController-删除车辆信息,id={}", vehicleId);
        vehicleService.deleteVehicle(vehicleId);
        return JsonResult.ok();
    }

    @GetMapping("/delete/{vehicleId}")
    public JsonResult deleteVehicles(@PathVariable("vehicleId") Long vehicleId){
        log.info("VehicleController-删除车辆信息,id={}", vehicleId);
        vehicleService.deleteVehicle(vehicleId);
        return JsonResult.ok();
    }

    @PostMapping("/unbindVehicle/{vehicleId}")
    public JsonResult unbindVehicle(@PathVariable("vehicleId")  Long vehicleId){
        log.info("VehicleController-解除当前电子围栏绑定的编号为{}的车辆信息的方法unbindVehicle()", vehicleId);
        vehicleService.unbindVehicle(vehicleId);
        return JsonResult.ok();
    }

    @PostMapping("/unbind/{vehicleId}")//此方法和上边的方法是重复的(一模一样)
    public JsonResult unbindVehicle2(@PathVariable("vehicleId")  Long vehicleId){
        log.info("VehicleController-解除当前电子围栏绑定的编号为{}的车辆信息的方法unbindVehicle()", vehicleId);
        vehicleService.unbindVehicle(vehicleId);
        return JsonResult.ok();
    }

    @PostMapping("/bindVehicle/{geofenceId}/{vehicleId}")
    public JsonResult bindVehicle(@PathVariable("geofenceId")  Long geofenceId, @PathVariable("vehicleId")  Long vehicleId){
        log.info("VehicleController-bindVehicle()-将编号为{}的电子围栏和编号为{}的车辆进行绑定", geofenceId,vehicleId);
        vehicleService.bindVehicle(geofenceId, vehicleId);
        return JsonResult.ok();
    }

    @PostMapping("/bind/{geofenceId}/{vehicleId}")//此方法和上边的方法是重复的(一模一样)
    public JsonResult bindVehicle2(@PathVariable("geofenceId")  Long geofenceId, @PathVariable("vehicleId")  Long vehicleId){
        log.info("VehicleController-bindVehicle()-将编号为{}的电子围栏和编号为{}的车辆进行绑定", geofenceId,vehicleId);
        vehicleService.bindVehicle(geofenceId, vehicleId);
        return JsonResult.ok();
    }

}
